package com.wangjie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangjie.mapper.EmployeeMapper;
import com.wangjie.pojo.Employee;
import com.wangjie.response.CommonCode;
import com.wangjie.response.QueryResponseResult;
import com.wangjie.response.QueryResult;
import com.wangjie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jieshao
 * @date 2021/5/21 17:13
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee selectById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public QueryResponseResult selectByPage(int page, int size) {
        if (page < 1) {
            page = 1;
        }

        if (size < 1) {
            size = 20;
        }

        PageInfo<Employee> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> {
            employeeMapper.selectByPage(new Employee());
        });

        QueryResult<Employee> queryResult = new QueryResult<>();
        queryResult.setList(pageInfo.getList());
        queryResult.setTotal(pageInfo.getTotal());

        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }
}